package com.qing.web_freemarke.listener;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 获取Excel导入的数据
 */
public class ImportDataList {

	public static <T> List<T> getImportDateList(MultipartFile file, Class<T> clazz) {
		ExcelImportDataListener excelImportDataListener = new ExcelImportDataListener<T>();
		ExcelReader excelReader = null;
		try {
			excelReader = EasyExcel.read(file.getInputStream(), clazz, excelImportDataListener).build();
			ReadSheet readSheet = EasyExcel.readSheet(0).build();
			excelReader.read(readSheet);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (excelReader != null) {
				// 这里千万别忘记关闭，读的时候会创建临时文件，到时磁盘会崩的
				excelReader.finish();
			}
		}
		return excelImportDataListener.getList();
	}


}
